from flask import Flask, request, render_template
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,IntegerField,RadioField,SubmitField
from wtforms.validators import DataRequired,EqualTo
app=Flask(__name__)
app.config["SECRET_KEY"]="123"

class Register(FlaskForm):
    user_name=StringField(label="用户名",validators=[DataRequired("用户名不能为空")])
    password=PasswordField(label="用户名",validators=[DataRequired("密码不能为空")])
    password2 = PasswordField(label="用户名", validators=[DataRequired("密码不能为空"),EqualTo("password",message="两次密码不相等")])
    age=IntegerField(label="年龄",validators=[DataRequired("年龄不能为空")])
    gender=RadioField(choices=[("gender1","男"),("gender0","女")],label="性别")
    address=StringField(label="用户名",validators=[DataRequired("地址不能为空")])
    submit = SubmitField(label="注册")


@app.route("/register",methods=["GET","POST"])
def register():
    form = Register()
    if request.method=="GET":
        return render_template("register2.html",form=form)
    if request.method=="POST":

        if form.validate_on_submit():
            username=form.user_name.data
            password=form.password.data
            password2=form.password2.data
            age=form.age.data
            gender=form.gender.data
            address=form.address.data
            
            print(username, password, password2, age, gender, address)
        else:
            print("验证失败")
            return render_template("register2.html",form=form)

        return "数据接受成功"

if __name__ == '__main__':
    app.run()